package cn.com.wangluotongxin.service.utils;

import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TengXunUtil {


    private static final String OUTBACK_CODE = "+86";

    /**
     * 发送短信
     */
    public static void sendSms(String secretId, String secretKey, String appId, String templateId, String signName, String phone, String context) throws TencentCloudSDKException {
        SmsClient client = smsClient(secretId, secretKey);
        SendSmsRequest request = new SendSmsRequest();
        String[] phoneNumberSet = new String[] { OUTBACK_CODE.concat(phone) };
        request.setPhoneNumberSet(phoneNumberSet);
        request.setSmsSdkAppid(appId);
        request.setSign(signName);
        request.setTemplateID(templateId);
        request.setSessionContext(context);
        SendSmsResponse response = client.SendSms(request);
        log.info("腾讯短信发送响应:{}", JSONObject.toJSONString(response));
    }

    private static SmsClient smsClient(String secretId, String secretKey){
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(secretId, secretKey);
        return new SmsClient(cred, "", clientProfile("sms.tencentcloudapi.com"));
    }

    private static ClientProfile clientProfile(String endpoint){
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(endpoint);
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        return clientProfile;
    }
}
